package com.xiege.springboot.controller;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xiege.springboot.common.Result;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;

import com.xiege.springboot.service.IWayService;
import com.xiege.springboot.entity.Way;

import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 榭歌
 * @since 2023-03-04
 */
@RestController
@RequestMapping("/way")
public class WayController {

    @Resource
    private IWayService wayService;

    // 新增或者更新
    @PostMapping
    public Result save(@RequestBody Way way) {
        wayService.saveOrUpdate(way);
        return Result.success();
    }

    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        wayService.removeById(id);
        return Result.success();
    }

    @PostMapping("/del/batch")
    public Result deleteBatch(@RequestBody List<Integer> ids) {
        wayService.removeByIds(ids);
        return Result.success();
    }

    @GetMapping
    public Result findAll() {
        return Result.success(wayService.list());
    }

    @GetMapping("/{id}")
    public Result findOne(@PathVariable Integer id) {

//        return Result.success(wayService.getById(id));
        return Result.success(wayService.findWayById(id));
    }

    @GetMapping("/page")
    public Result findPage(@RequestParam Integer pageNum,
                                @RequestParam Integer pageSize,
                           @RequestParam String name) {
        QueryWrapper<Way> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("view_count");
        if (StrUtil.isNotBlank(name)) {
            queryWrapper.like("wayname", name);
        }
        return Result.success(wayService.page(new Page<>(pageNum, pageSize), queryWrapper));
    }

}

